package com.shou.trigger.http;

import com.shou.api.model.dto.DataAnalysisDTO;
import com.shou.api.model.response.ResultData;
import com.shou.domain.park.service.IParkingLotService;
import com.shou.domain.park.service.IParkingRecordService;
import com.shou.domain.park.service.IParkingSpaceService;
import com.shou.trigger.http.mapstruct.common.DataAnalysisDTOConvert;
import com.shou.types.utils.TimeUtil;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * ClassName:DataAnalysisController
 * Project:SmartPark
 * Package: com.shou.trigger.http
 * Description
 *
 * @Author Nemo
 * @Create 2025/2/21 20:33
 * @Version 1.0
 */
@Validated
@RestController
@CrossOrigin
@RequestMapping("/api/v1/data-analysis")
public class DataAnalysisController extends AbstructController {
    @Resource
    private IParkingLotService parkingLotService;
    @Resource
    private IParkingRecordService parkingRecordService;
    @Resource
    private IParkingSpaceService parkingSpaceService;
    @Resource
    private DataAnalysisDTOConvert dataAnalysisDTOConvert;
    /**
     * 分页查询
     */
    @GetMapping("/data-analysis-info")
    public ResultData<DataAnalysisDTO> analyze(@RequestParam Map<String, Object> analysisDTOMap){
        DataAnalysisDTO dataAnalysisDTO = DataAnalysisDTO.getDataAnalysisDTOFromMap(analysisDTOMap);
        // 入参校验
        validateDataAnalysisParams(dataAnalysisDTO);

        List<String> xAxisData = TimeUtil.getTimeUnits(dataAnalysisDTO.getStartTime(), dataAnalysisDTO.getEndTime(), dataAnalysisDTO.getTimeUnit());
        Long totalSpaces = parkingSpaceService.getSpacesAvailableInfoByCode(dataAnalysisDTO.getLotCode()).getTotalSpaces();

        dataAnalysisDTO.setLineXAxisData(xAxisData);
        dataAnalysisDTO.setBarXAxisData(xAxisData);
        dataAnalysisDTO.setInCounts(parkingRecordService.getInCountsByCodeAndStartTime(dataAnalysisDTO.getLotCode(), xAxisData, dataAnalysisDTO.getTimeUnit()));
        dataAnalysisDTO.setOutCounts(parkingRecordService.getOutCountsByCodeAndStartTime(dataAnalysisDTO.getLotCode(), xAxisData, dataAnalysisDTO.getTimeUnit()));
         if(totalSpaces != null && totalSpaces > 0) {
             dataAnalysisDTO.setOccupancyRates(parkingRecordService.getOccupancyRatesByCodeAndStartAndEndTime(dataAnalysisDTO.getLotCode(), xAxisData, dataAnalysisDTO.getTimeUnit(), totalSpaces));
         }
//      incomeRatio(parkingRecordService.getIncomeRatioByCodeAndStartTime(dataAnalysisDTO.getLotCode(), dataAnalysisDTO.getStartTime())))
        return ResultData.success(dataAnalysisDTO);
    }

    @GetMapping("/data-analysis-income")
    public ResultData<DataAnalysisDTO> getTotalIncome(@RequestParam Map<String, Object> analysisDTOMap){
        DataAnalysisDTO dataAnalysisDTO = DataAnalysisDTO.getDataAnalysisDTOFromMap(analysisDTOMap);
        // 入参校验
        validateDataAnalysisParams(dataAnalysisDTO);
        dataAnalysisDTO.setTotalIncome(parkingRecordService.getTotalIncomeByCodeAndStartTime(dataAnalysisDTO.getLotCode(), dataAnalysisDTO.getStartTime(), dataAnalysisDTO.getEndTime()));
        return ResultData.success(dataAnalysisDTO);
    }
}
